package thread.wang_bao_ling.c21;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * 演示AtomicIntegerFieldUpdater
 */
public class AtomicIntegerFieldUpdaterest {

    //注意id字段需要有volatile修饰
    static AtomicIntegerFieldUpdater<User> updater = AtomicIntegerFieldUpdater.newUpdater(User.class,"id");

    public static void main(String[] args)   {
        User user1=new User(1);
        boolean u1=updater.compareAndSet(user1, 1, 2);
        //输出user1中id的值
        System.out.println( updater.get(user1));
        System.out.println(u1);
        boolean u2=updater.compareAndSet(user1, 1, 2);
        //输出user1中id的值
        System.out.println( updater.get(user1));


    }
}
